import { createStore } from "vuex";
import router from '../router'
import { ElMessage,ElMessageBox } from 'element-plus'
import { HttpLogout } from '../public/http'
export default createStore({
  state: {
	userInfoX: null as any,
	webSocketX:null as any,
	battleInfoX:null,
	battleMessageX:[] as any
  },
  getters: {},
  mutations: {
	
	userInfoMain(state,res){
		state.userInfoX = JSON.parse(res)
		localStorage.setItem("userInfoX",res)
	},
	sockerMain(state,res){
		state.webSocketX = res
		localStorage.setItem("webSocketX",JSON.stringify(res))
	},
	// 修改对战信息
	battleInfoMain(state,res){
		state.battleInfoX = res
		localStorage.setItem("battleInfoX",JSON.stringify(res))
	},
	// 对战人物信息
	battleMessageMain(state,res){
		state.battleMessageX.push(res)
		localStorage.setItem("battleMessageX",JSON.stringify(state.battleMessageX))
	},
	clearBattleMessageMain(state,res){
		state.battleMessageX = []
		localStorage.removeItem("battleMessageX")
	},
	isLogoutMain(state){
		if(!state.userInfoX || (state.userInfoX&&!state.userInfoX.token)){
			ElMessage({
				message: '你还未登录，请登录后再进行操作!',
				type: 'warning',
			})
			// ElMessageBox.confirm(
			// 	'你还未登录，请登录后再进行操作?',
			// 	'警告',
			// 	{
			// 	  confirmButtonText: '去登陆',
			// 	  cancelButtonText: '取消',
			// 	  type: 'warning',
			// 	}
			//   ).then(() => {
			// 	router.push('/login')
							
			// }).catch(()=>{
				
			// })
		}
	},
	logoutMain(state,res){
		if(res === 401){
			state.userInfoX = null
			sessionStorage.removeItem("navType")
			sessionStorage.removeItem("navSelect")
			localStorage.removeItem("userInfoX")
			return
		}
		if(res){
			HttpLogout().then((data:any)=>{
				ElMessage({
				    message: res === 1?'您的账号已在别处登录！':data.message,
				    type: 'warning',
				})
				router.push('/login')
			})
		}else{
			
			router.push('/login')
		}
		
		state.userInfoX.token = null
		sessionStorage.removeItem("navType")
		sessionStorage.removeItem("navSelect")
		localStorage.removeItem("webSocketX")
		localStorage.removeItem("battleInfoX")
		localStorage.setItem("userInfoX",JSON.stringify(state.userInfoX))
		if(state.webSocketX){
			state.webSocketX.close()
		}
		
	}
	  
  },
  actions: {
	  
  },
  modules: {},
});
